package com.codingman.jdbcdemo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//transaction
public class Demo5 {

	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return;
		}

		Connection con = null;
		try {
			con = DriverManager.getConnection("jdbc:mysql://45.77.187.28:3306/codingdb", "codingman",
					"Cm@123456");
		} catch (SQLException e1) {
			e1.printStackTrace();
			return;
		}
		
		try {
			con.setAutoCommit(false);
			
			PreparedStatement stmt=con.prepareStatement("insert into employee values(?,?,?)");  
			stmt.setInt(1, 105);
			stmt.setString(2, "Five");
			stmt.setFloat(3, 500);
			
			int i = stmt.executeUpdate();  
			System.out.println(i+" records inserted");  
			
			con.commit();
			con.close();

		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}

	}

}
